package com.ninesevenseven;

public class NineSevenSeven {

    public int[] sortedSquares(int[] nums) {
        int n = nums.length - 1, right = n, pos = n;
        int[] res = new int[nums.length];

        for (int i = 0; i <= right; ) {
            if (nums[i] * nums[i] > nums[right] * nums[right]) {
                res[pos] = nums[i] * nums[i];
                ++i;
            } else {
                res[pos] = nums[right] * nums[right];
                --right;
            }
            --pos;
        }
        return res;
    }
}
